using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 击退Buff - 将目标击退一定距���
/// </summary>
public class KnockbackBuff : BuffBase
{
    private Vector3 m_KnockbackDirection;
    private float m_KnockbackDistance;
    private float m_KnockbackSpeed;
    private Vector3 m_StartPosition;
    private Vector3 m_TargetPosition;
    private bool m_IsKnockbacking;

    public KnockbackBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is KnockbackData knockbackData)
        {
            m_KnockbackDistance = knockbackData.Distance;
            m_KnockbackSpeed = knockbackData.Speed;
        }
        else
        {
            m_KnockbackDistance = 2.0f;
            m_KnockbackSpeed = 10.0f;
        }
        
        // 计算击退方向（从施法者到目标���
        if (Caster != null)
        {
            m_KnockbackDirection = (Target.transform.position - Caster.transform.position).normalized;
        }
        else
        {
            m_KnockbackDirection = Vector3.right; // 默认向右击退
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        m_StartPosition = Target.transform.position;
        m_TargetPosition = m_StartPosition + m_KnockbackDirection * m_KnockbackDistance;
        m_IsKnockbacking = true;
        
        // 播放击退特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("knockback", true);
        }
        
        Logger.Log($"{Target.name} 被击退���{m_KnockbackDistance} ���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        if (m_IsKnockbacking)
        {
            // 执行击退移动
            Vector3 currentPos = Target.transform.position;
            Vector3 newPos = Vector3.MoveTowards(currentPos, m_TargetPosition, m_KnockbackSpeed * dt);
            Target.transform.position = newPos;
            
            // 检查是否到达目标位���
            if (Vector3.Distance(newPos, m_TargetPosition) < 0.1f)
            {
                m_IsKnockbacking = false;
                Target.transform.position = m_TargetPosition;
                
                // 恢复动画状���
                if (Target.Animator != null)
                {
                    Target.Animator.SetBool("knockback", false);
                }
            }
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 击退效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Knockback"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 击退效果不能叠加，只能刷新持续时���
    }
}
